package com.hzlj.position.locate.dao;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Pair;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fz.common.base.converter.QueryWrapperConverterPlus;
import com.fz.common.base.core.JsonUtils;
import com.fz.common.base.dto.PageParamDTO;
import com.fz.common.base.dto.QueryPageResultDTO;
import com.fz.common.base.dto.SortParamDTO;
import com.fz.common.base.exception.PubError;
import com.fz.common.base.exception.PubException;
import com.hzlj.position.locate.common.dto.reportCommon.ReportHandleDTO;
import com.hzlj.position.locate.common.dto.reportInitiative.ReportInitiativeQueryParamDTO;
import com.hzlj.position.locate.common.dto.reportInitiative.ReportInitiativeWithJzJbxxDTO;
import com.hzlj.position.locate.common.model.ReportInitiative;
import com.hzlj.position.locate.mapper.ReportInitiativeMapper;
import com.hzlj.sqjz.config.common.dto.common.handle.HandleCommonQueryResultDTO;
import com.hzlj.sqjz.config.common.enums.HandleStatus;
import com.hzlj.sqjz.jz.common.constant.JzJoin;
import com.hzlj.sqjz.jz.common.model.JzJbxx;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 通信联络核查-人脸签到(ReportInitiative)表操作类
 *
 * @author lifh
 * @date 2023-03-13 19:10:13
 */
@Repository
public class ReportInitiativeDao extends ServiceImpl<ReportInitiativeMapper, ReportInitiative> {
    @Resource
    private ReportInitiativeMapper reportInitiativeMapper;

    /**
     * 删除数据
     */
    public void delete(String jzId, Date now) {
        LambdaUpdateWrapper<ReportInitiative> deleteWrapper = new UpdateWrapper<ReportInitiative>().lambda()
                .eq(ReportInitiative::getJzId, jzId)
                .eq(ReportInitiative::getBelongDay, now);
        this.remove(deleteWrapper);
    }

    public boolean updateReportInitiative(ReportInitiative reportInitiative) {
        if (ObjectUtil.isEmpty(reportInitiative.getId())
                || ObjectUtil.isEmpty(reportInitiative.getBelongDay())) {
            throw new PubException(PubError.PARAM_EMPTY_ERROR);
        }

        LambdaUpdateWrapper<ReportInitiative> updateWrapper = new UpdateWrapper<ReportInitiative>().lambda()
                .eq(ReportInitiative::getId, reportInitiative.getId())
                .eq(ReportInitiative::getBelongDay, reportInitiative.getBelongDay());
        return this.update(reportInitiative, updateWrapper);
    }


    /**
     * 查询最近的点验记录
     */
    public boolean existsReportInitiativeByJzId(String jzId, Date now) {
        ReportInitiative initiative = QueryWrapperConverterPlus.of(reportInitiativeMapper)
                .selectOne(ReportInitiative.class, null, null, q -> {
                    q.select(ReportInitiative::getId)
                            .eq(ReportInitiative::getJzId, jzId)
                            .eq(ReportInitiative::getBelongDay, DateUtil.beginOfDay(now))
                    ;
                });
        return ObjectUtil.isNotEmpty(initiative)
                && ObjectUtil.isNotEmpty(initiative.getId());
    }

    /**
     * 查询最近的点验记录
     */
    public ReportInitiative getReportInitiativeByJzId(String jzId, Date now) {
        return QueryWrapperConverterPlus.of(reportInitiativeMapper)
                .selectOne(ReportInitiative.class, null, null, q -> {
                    q.eq(ReportInitiative::getJzId, jzId)
                            .eq(ReportInitiative::getBelongDay, DateUtil.beginOfDay(now))
                    ;
                });
    }

    public ReportInitiativeWithJzJbxxDTO getReportInitiative(String id, Date now) {
        return queryWrapper()
                .selectOne(ReportInitiativeWithJzJbxxDTO.class, null, null, q -> {
                    q.eq(ReportInitiative::getId, id)
                            .eq(ReportInitiative::getBelongDay, DateUtil.beginOfDay(now))
                    ;
                });
    }


    public List<ReportInitiativeWithJzJbxxDTO> listReportInitiative(ReportInitiativeQueryParamDTO dto) {
        return queryWrapper()
                .list(ReportInitiativeWithJzJbxxDTO.class, dto, sortParam());
    }


    /**
     * 查询列表：分页
     */
    public QueryPageResultDTO<ReportInitiativeWithJzJbxxDTO> pageReportInitiative(ReportInitiativeQueryParamDTO dto, PageParamDTO page) {
        page.setSortFns(sortParam().getSortFns());
        return queryWrapper()
                .page(ReportInitiativeWithJzJbxxDTO.class, dto, page);
    }


    public HandleCommonQueryResultDTO getHandleResult(String id, Date belongDay) {
        return QueryWrapperConverterPlus.of(reportInitiativeMapper)
                .selectOne(HandleCommonQueryResultDTO.class, null, null, q -> {
                    q.eq(ReportInitiative::getId, id)
                            .eq(ReportInitiative::getBelongDay, belongDay);
                });
    }


    public void handle(ReportHandleDTO dto) {
        Pair<Date, Date> dateDatePair = dto.minMaxBelongDay();
        LambdaUpdateWrapper<ReportInitiative> updateWrapper = new UpdateWrapper<ReportInitiative>().lambda()
                .set(ReportInitiative::getHandleStatus, dto.getHandleStatus())
                .set(ReportInitiative::getHandleTime, dto.getHandleTime())
                .set(ObjectUtil.isNotEmpty(dto.getHandleExt()), ReportInitiative::getHandleExt, JsonUtils.obj2json(dto.getHandleExt()))
                .in(ReportInitiative::getId, dto.ids())
                .ge(ReportInitiative::getBelongDay, dateDatePair.getKey())
                .le(ReportInitiative::getBelongDay, dateDatePair.getValue());
        this.update(updateWrapper);
    }


    public void updateHandleStatus(String id,
                                   Date belongDay,
                                   HandleStatus handleStatus) {
        LambdaUpdateWrapper<ReportInitiative> updateWrapper = new UpdateWrapper<ReportInitiative>().lambda()
                .set(ReportInitiative::getHandleStatus, handleStatus.getStatus())
                .eq(ReportInitiative::getId, id)
                .eq(ReportInitiative::getBelongDay, belongDay);
        this.update(updateWrapper);
    }


    private QueryWrapperConverterPlus<ReportInitiative> queryWrapper() {
        return QueryWrapperConverterPlus.of(reportInitiativeMapper)
                .innerJoin(JzJbxx.class, JzJoin.jz(ReportInitiative::getJzId));
    }

    private SortParamDTO sortParam() {
        return SortParamDTO.of(false, ReportInitiative::getReportTime)
                .and(false, ReportInitiative::getId);

    }

}

